package com.yoofn.kotlin.base

import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.widget.SwipeRefreshLayout
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.yoofn.kotlin.R
import com.yoofn.nbwk.base.HttpFramework

/**
 *
 * Created by kalshen on 17/6/2.
 * @描述 所有fragment 的基础类,可以设置懒加载
 */
abstract class BaseFragment<Binding : ViewDataBinding, FirstRequestType : Any> : Fragment() ,HttpFramework{

    /**
     * fragment的配置参数
     */
    val viewConfig by lazy {
        setupConfig()
    }

    /**
     * fragment的布局资源id
     */
    val layoutId by lazy {
        viewConfig.layoutId
    }

    /**
     * 是否加载完成
     * 当执行完onCreateView,View的初始化方法后方法后即为true
     */
    val shouldLazyLoad by lazy {
        viewConfig.shouldLazyLoad
    }

    val shouldSwipeRefresh by lazy {
        viewConfig.shouldSwipeRefresh
    }

    /**
     * 网络请求对象
     * fragment 在网络访问时初始化的请求的封装对象
     */
    val netData by lazy {
        viewConfig.netData
    }

    /**
     * 下拉刷新控件
     */
    val swipeRefresh by lazy {
        SwipeRefreshLayout(activity)
    }

    /**
     * 具有dataBinding 数据绑定特性的 contentView
     * 可以直接调用被layout 包裹的 控件资源id
     * 例:content.textView.text="hello"
     * 不懂的 自己去dataBinding
     */
    lateinit var contentView: Binding


    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?
                              , savedInstanceState: Bundle?): View? {

        var view = inflater?.inflate(layoutId, container, false)
        contentView = DataBindingUtil.bind<Binding>(view)
        //不是懒加载直接请求网络
        addRequest(netData.takeIf { !GlobalConfig.COULD_SCROLL_HORIZONTAL })
        val view2 = if (shouldSwipeRefresh) addSwipeRefresh(view) else view
        doInteractive()
        return view2
    }

    /**
     * 为fragment 添加下拉刷新 并且该刷新是自动的
     * 也可以 取消自动属性,自定义一个空方法 让子类去重写
     * @param view 该view 是由布局文件填充出来的view
     */
    private fun addSwipeRefresh(view: View?): View {
        return swipeRefresh.apply {
            addView(view)
            //swipeRefreshLayout的颜色默认设置成系统主题的颜色
            setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary
                    , R.color.colorPrimaryDark)
            setOnRefreshListener {
                if (swipeRefresh.isRefreshing) {
                    //下拉刷新再次请求第一次的接口
                    addRequest(netData)
                }
            }
        }
    }


    /**
     * 配置fragment 的基本特征
     * 例:是否懒加载，netData...
     */
    abstract fun setupConfig(): ViewConfig<FirstRequestType>

    /**
     * 配置ui与业务逻辑的交互
     */
    abstract fun doInteractive()

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (shouldLazyLoad && isVisibleToUser) {    //判断用户是否要懒加载并且界面是否可见
            addRequest(netData)     //访问网络加载数据
        }
    }



    override fun onRequestFinished(what: Int) {
        swipeRefresh.isRefreshing = false
    }

    /**
     * 开启一个webActivity
     */
    fun startWebActivity(url: String) {
        (activity as BaseActivity<*, *>).startWebActivity(url)
    }

    /**
     * 开启一个replaceActivity
     */
    fun startReplaceActivity() {
        (activity)
    }

    /**
     * 开启一个replaceActivity
     */
    fun startReplaceActivity(bundle: Bundle) {
        (activity as BaseActivity<*, *>).startReplaceActivity(bundle)
    }
}